/*
 * comms.cpp
 *
 *  Created on: Mar 11, 2011
 *      Author: B. Jones
 */

#include "comms.h"

theEncryptor* comms::encryptor=0;
fileTransferer* comms::ft=0;
std::vector<com_params> comms::all_com_params;
std::vector<peer_info> comms::all_peer_infos;

comms::comms()
{

}

void comms::sendNick(std::string charNick,com_params &cp)
{
	int length = charNick.length();
	std::cout<<"Length charNick server: "<<length<<std::endl;
	int n = send(cp.sockChat,charNick.c_str(),length,0);
	std::cout<<n<<" bytes sent of charNick in server"<<std::endl;
}

void
comms::doFileTransfer(const char* filePath, const char* filename)
{
	// note -- here the link id is assumed to be 0 since currently only
	// once connection is possible
	cm->initiateFileTransfer(0, filePath, filename);
}

void
comms::doFileReceive(const char* savePath, bool yesno)
{
	// note -- here the link id is assumed to be 0 since currently only
	// once connection is possible
	cm->initiateFileReceive(0, savePath,yesno);
}


void
comms::sendMessage(std::string str)
{
	// send message to all connected peers (this is for server side of things)
	// note server will also handle distribution of messages from one client to all other clients

	// append a 'C' to front of message (inidicates chat mode -- protocol)
	std::stringstream ss;
	ss<<"C"<<str;
	std::string ss_str = ss.str();

	const char *chars = ss_str.c_str();
	for(int connection=0;connection<all_com_params.size();connection++)
	{
		string str_(chars);
		std::cout<<"Sending str_ message: "<<str_<<std::endl;
		pthread_mutex_lock(&channelManager::sendMutex);
		channelManager::sendMessage(str_,connection);
		pthread_mutex_unlock(&channelManager::sendMutex);
	}
}

void
comms::initiateChannelManager()
{
	cm = new channelManager;
	encryptor = new theEncryptor;
	ft = new fileTransferer;
}

void
comms::pushEvent()
{

}
